package 线性表;
import java.util.*;;
public class SequenceList<T> implements Iterable<T>{
    private T[] any;
    private int N;
    public SequenceList(int capacity) {
        this.any=(T[])new Object[capacity];
        this.N=0;
    }
    public void clear(){
        this.N=0;
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int length(){
        return N;
    }
    public T get(int i) {
        return any[i];
    }
    public void insert(int i,T t){
        if(N==any.length){
            resize(2*any.length);
        }
        for(int index=N;index>=i;index--){
            any[index+1]=any[index];
        }
        any[i]=t;
        N++;
    }
    public void insert(T i) {
        if(N==any.length){
            resize(2*any.length);
        }
        any[N++]=i;
    }
    public T remove(int i) {
        T current=any[i];
        for(int index = i;i<N-1;i++){
            any[index]=any[index+1];
        }
        N--;
        if(N<any.length/4){
            resize(any.length/2);
        }
        return current;

    }
    public int indexOf(T t){
        for(int i=0;i<N-1;i++){
            if(any[i].equals(t)){
                return i;
            }
        }
        return -1;
    }

    public void resize(int newSize){
        T[] temp = any;
        any=(T[])new Object[newSize];
        for(int i=0;i<N;i++){
            any[i]=temp[i];
        }
    }

















    @Override
    public Iterator<T> iterator(){
        return new SIterator();
    }
    public class SIterator implements Iterator{
        private int cusor;
        public SIterator(){
            this.cusor=0;
        }
        @Override
        public boolean hasNext(){
            return cusor<N;
        }
        @Override
        public Object next(){
            return any[cusor++];
        }
    }
    
}